#!/bin/bash
#ods_to_dim.sh all/表名 [日期]

# 日志输出函数
log() {
    local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
    echo "[$timestamp] $1"
}

# 1、判断参数是否传入
if [ $# -lt 1 ]; then
    log "必须传入all/表名..."
    exit 1
fi

# 2、判断日期是否传入，传入则加载指定日期的数据，没有传入则加载前一天日期数据
[ "$2" ] && datestr=$2 || datestr=$(date -d '-1 day' +%F)
log "当前处理日期: $datestr"

dim_doctor_full_sql="
with dc as (
    select 
        id,
        birthday,
        consultation_fee,
        TRIM(gender) as gender,
        name,
        TRIM(specialty) as specialty,
        TRIM(title) as title,
        hospital_id
    from ods_doctor_full 
    where dt = '${datestr}'
),
dic as (
    select 
        TRIM(id) as id,
        value
    from ods_dict_full 
    where dt = '${datestr}'
)
insert overwrite table dim_doctor_full partition (dt = '${datestr}')
select 
    dc.id,
    birthday,
    consultation_fee,
    gender,
    dic1.value,
    name,
    specialty,
    dic2.value,
    title,
    dic3.value,
    hospital_id
from dc 
left join dic dic1 on dc.gender = dic1.id
left join dic dic2 on dc.specialty = dic2.id
left join dic dic3 on dc.title = dic3.id;
"

dim_hospital_full="
insert overwrite table dim_hospital_full partition (dt='${datestr}')
select id,
       address,
       alias,
       bed_num,
       city,
       department_num,
       district,
       establish_time,
       health_care_num,
       insurance,
       level,
       name,
       province
from ods_hospital_full where dt='${datestr}';
"

dim_medicine_full="
insert overwrite table dim_medicine_full partition (dt='${datestr}')
select id,
       approval_code,
       dose_type,
       name,
       name_en,
       price,
       specs,
       trade_name
from ods_medicine_full where dt='${datestr}';
"

dim_patient_full="
with pt as(
    select 
        id,
        TRIM(birthday) as birthday,
        TRIM(gender) as gender,
        TRIM(name) as name,
        user_id
    from ods_patient_full 
    where dt='${datestr}'
),
bc as (
    select
        TRIM(id) as id,
        value
    from ods_dict_full 
    where dt='${datestr}'
)
insert overwrite table dim_patient_full partition (dt='${datestr}')
select
    pt.id,
    birthday,
    gender,
    bc.value,
    name,
    user_id
from pt 
left join bc on pt.gender = bc.id;
"

dim_user_full="
insert overwrite table dim_user_full partition (dt='${datestr}')
select
    id,
    TRIM(email) as email,
    TRIM(telephone) as telephone,
    TRIM(username) as username
from ods_user_full where dt='${datestr}';
"

# 3、根据第一个参数匹配加载表的数据
case $1 in
"all")
    log "开始处理所有表数据..."
    /opt/module/hive/bin/hive -e "use medical;${dim_doctor_full_sql};${dim_hospital_full};${dim_medicine_full};${dim_patient_full};${dim_user_full}"
    if [ $? -eq 0 ]; then
        log "所有表数据处理完成。"
    else
        log "处理所有表数据时出现错误。"
        exit 1
    fi
    ;;
"dim_doctor_full")
    log "开始处理 dim_doctor_full 表数据..."
    /opt/module/hive/bin/hive -e "use medical;${dim_doctor_full_sql}"
    if [ $? -eq 0 ]; then
        log "dim_doctor_full 表数据处理完成。"
    else
        log "处理 dim_doctor_full 表数据时出现错误。"
        exit 1
    fi
    ;;
"dim_hospital_full")
    log "开始处理 dim_hospital_full 表数据..."
    /opt/module/hive/bin/hive -e "use medical;${dim_hospital_full}"
    if [ $? -eq 0 ]; then
        log "dim_hospital_full 表数据处理完成。"
    else
        log "处理 dim_hospital_full 表数据时出现错误。"
        exit 1
    fi
    ;;
"dim_medicine_full")
    log "开始处理 dim_medicine_full 表数据..."
    /opt/module/hive/bin/hive -e "use medical;${dim_medicine_full}"
    if [ $? -eq 0 ]; then
        log "dim_medicine_full 表数据处理完成。"
    else
        log "处理 dim_medicine_full 表数据时出现错误。"
        exit 1
    fi
    ;;
"dim_patient_full")
    log "开始处理 dim_patient_full 表数据..."
    /opt/module/hive/bin/hive -e "use medical;${dim_patient_full}"
    if [ $? -eq 0 ]; then
        log "dim_patient_full 表数据处理完成。"
    else
        log "处理 dim_patient_full 表数据时出现错误。"
        exit 1
    fi
    ;;
"dim_user_full")
    log "开始处理 dim_user_full 表数据..."
    /opt/module/hive/bin/hive -e "use medical;${dim_user_full}"
    if [ $? -eq 0 ]; then
        log "dim_user_full 表数据处理完成。"
    else
        log "处理 dim_user_full 表数据时出现错误。"
        exit 1
    fi
    ;;
*)
    log "表名输入错误..."
    exit 1
    ;;
esac